package com.mazegame.backend;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class BackgroundMusic {
    private static Clip clip;

    public static void playMusic(String filePath) {
        try {
            File musicFile = new File(filePath);
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(musicFile);
            clip = AudioSystem.getClip();
            clip.open(audioIn);

            // 设置循环播放
            clip.loop(Clip.LOOP_CONTINUOUSLY);

            // 启动播放线程
            new Thread(() -> {
                clip.start();
            }).start();

        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public static void stopMusic() {
        if (clip != null && clip.isRunning()) {
            clip.stop();
        }
    }
}
